import '../utils/tools.dart';
import 'comparable.dart';
import 'serializable.dart';

class ImageModel extends Serializable implements EquableMulti {
  int id = 0;
  String title = '';
  String image = '';
  String video = '';
  String description = '';
  int sort = 0;

  final String type;
  int belongId = 0;

  ImageModel.fromJson(Map image, {this.type = 'article'}) {
    id = Tools.parseInt(image['id']);
    title = image['title'] ?? '';
    description = image['description'] ?? '';
    this.image = Tools.fixImageUrl(image['image']);
    video = Tools.fixImageUrl(image['video']);
    sort = Tools.parseInt(image['sort']);

    if (type == 'article') {
      belongId = image['article_id'];
    } else if (type == 'product') {
      belongId = image['product_id'];
    }
  }

  @override
  Map toJson() {
    return {
      'id': id,
      'title': title,
      'image': image,
      'video': video,
      'description': description,
      'sort': sort,
      '${type}_id': belongId,
    };
  }

  @override
  List get compareFields => [id, type, belongId];
}
